package com.jicheng.mapper;

import com.jicheng.dto.sysUser.*;
import com.jicheng.entity.SysRole;
import com.jicheng.entity.SysUser;
import com.jicheng.ext.dto.SearchPagingParams;
import com.jicheng.framework.mybatis.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysUserMapper extends BaseMapper<SysUser> {
    /**
     * 获得密码
     * @param username 用户名
     */
    String getPassword(String username);

    /**
     * 获得角色权限
     * @param username 用户名
     * @return user/admin
     */
    String getRole(String username);

    /**
     * 修改密码
     */
    void updatePassword(@Param("userId") String userId, @Param("newPassword") String newPassword);

    /**
     * 获得存在的用户
     */
    List<String> getUser();

    /**
     * 获得用户角色对应的权限
     */
    List<RolePermiDto> getRolePermission(SearchPagingParams searchPagingParams);

    /**
     * 获得用户的权限
     */
    String getPermission(String username);


    void addRole(@Param("sysRole") SysRoleParams sysRole);

    @Select("select id,role from sys_role")
    List<RoleDto> getRoleList();

    List<SysUserDto> userList(SearchPagingParams searchPagingParams);

    void setPermisson(@Param("rolePermiParams") RolePermiParams rolePermiParams);

    @Select("select DISTINCT p.id,p.name,p.url from sys_role_permis_children rp,sys_permission p where rp.permission_id = p.id and rp.role_id = #{roleId}")
    List<PermiforRoleDto> getPermiforRole(@Param("roleId") String roleId);

    @Update("update sys_user set role_id = #{roleId} where uid = #{userId}")
    void updateRole(@Param("userId") String userId, @Param("roleId") String roleId);

    @Select("select uid,username,card_id cardId,create_time createTime,phone,role_id roleId from sys_user where username = #{username}")
    UserInfo getUserInfo(@Param("username") String username);

    /**
     * 删除用户
     * @param userId
     */
    @Delete("delete from sys_user where uid = #{userId}")
    void delUser(@Param("userId") int userId);

    /**
     * 重置密码
     * @param userId
     * @param password
     */
    @Update("update sys_user set password = #{password} where uid = #{userId}")
    void resetPass(@Param("userId") String userId,@Param("password") String password);

    /**
     * 当删除角色时候，解除用户角色关系
     * @param id
     */
    @Delete("delete from sys_user_role where role_id = #{id}")
    void delUserRole(@Param("id") int id);

    /**
     * 当删除用户时候，解除用户角色关系
     * @param id
     */
    @Delete("delete from sys_user_role where uid = #{id}")
    void delRoleUser(@Param("id") int id);
    /**
     * 解除角色权限关系
     * @param roleId
     */
    @Delete("delete from sys_role_permis_children where role_id = #{roleId}")
    void delRolePermis(@Param("roleId") int roleId);

    /**
     * 删除角色
     * @param roleId
     */
    @Delete("delete from sys_role where id = #{roleId}")
    void delRole(int roleId);

    /**
     * 根据一级权限获取子权限
     * @param parentId
     * @return
     */
    @Select("select * from sys_permission_children where id in (select children_id childrenId from sys_role_permis_children where parent_id = #{parentId} and role_id = #{roleId}) ")
    List<ChildrenPermisDto> getPermiforChildren(@Param("parentId") int parentId,@Param("roleId") int roleId);

    /**
     * 获取所有菜单
     * @param parentId
     * @return
     */
    @Select("select id,name from sys_permission_children where parent_id = #{parentId} ")
    List<ChildrenPermisDto> getPermiforChildrenAll(@Param("parentId") int parentId);

    /**
     * 获取当前用户的一级菜单权限
     * @param username
     * @return
     */
    @Select("select DISTINCT p.id,p.name,rp.role_id roleId from sys_role_permis_children rp ,sys_permission p where p.id = rp.permission_id and rp.role_id = (select role_id from sys_user where username = #{username})")
    List<Menu> getMenu(@Param("username") String username);




    void addPermission(@Param("sysPermiParams") SysPermiParams sysPermiParams);

    @Select("select id,name from sys_permission")
    List<PermiforRoleDto> getAllPermission();
}
